#include "Bullet.h"
#include "../Game/Level.h"
#include "../Graphic/GraphicManager.h"
#include "../Graphic/TextureManager.h"
#include "Explosion.h"
#include <vector>
#include <math.h>

//Update Bullet position
void cBullet::Update(float lfTimeStep)
{
   //To control the speed of the player you can only move every 0.075ms
  mfElapsedTime += fabs(lfTimeStep );
  if (mfElapsedTime < 0.0525) return;
  mfElapsedTime = 0.0f;
  
  int liDirX = 0;
  int liDirY = 0;
  switch (miOrientation)
  {
    case eO_North:
      liDirX = 0;
      liDirY = -1;
    break;
    case eO_South:
      liDirX = 0;
      liDirY = 1;
    break;
    case eO_West:
      liDirX = -1;
      liDirY = 0;
    break;
    case eO_East:
      liDirX = 1;
      liDirY = 0;
    break;
    case eO_SouthEast:
      liDirX = 1;
      liDirY = 1;
    break;
    case eO_SouthWest:
      liDirX = -1;
      liDirY = 1;
    break;
    case eO_NorthEast:
      liDirX = 1;
      liDirY = -1;
    break;
    case eO_NorthWest:
      liDirX = -1;
      liDirY = -1;
    break;
  }

  unsigned int luiX = muiX + liDirX ;
  unsigned int luiY = muiY + liDirY ;
  if ((liDirY == - 1) && (luiY> muiY))
    {
      cLevel::Get().RemoveObject(macName);
      return;
    }

  if ((liDirX == - 1) && (luiX> muiX))
    {
      cLevel::Get().RemoveObject(macName);
      return;
    }
  

  cPlayer * lcPlayer = (cPlayer *)cLevel::Get().GetPlayer();
  
  //Detect collision with enemies
  if (meOwner == ePlayer)
  {
    std::vector<cObject *> lvEnemies = cLevel::Get().GetEnemies();
    for (std::vector<cObject *>::iterator it = lvEnemies.begin(); it!= lvEnemies.end(); it++)
    {
      if ( ((*it)->GetOrientation(luiX, luiY) == eO_Over) || ((*it)->GetOrientation(muiX,muiY) == eO_Over))
      {
        lcPlayer->SetPoints(lcPlayer->GetPoints() + 50);
        cLevel::Get().RemoveObject((*it)->GetName());
      
        //Draw explosion
        cObject * lcExplosion;
        if ((*it)->GetOrientation(muiX, muiY) == eO_Over)
          lcExplosion = new cExplosion(muiX, muiY);
        else
          lcExplosion = new cExplosion(luiX, luiY);
        cLevel::Get().InsertObject(lcExplosion);  
        cLevel::Get().RemoveObject(macName);
 
        return;
      }
    }
  }

  //Detect collision with player
  if (meOwner == eEnemy)
  {
    if ( ((lcPlayer)->GetOrientation(luiX, luiY) == eO_Over) ||
         ((lcPlayer)->GetOrientation(muiX,muiY) == eO_Over))
      {
        cLevel::Get().RemoveObject(macName);
        lcPlayer->Kill();
        return;
      }
  }

  //Update frame or Remove bullet if collide with a wall
  if ((!cLevel::Get().IsWall(luiX, luiY)) &&  (!cLevel::Get().IsWall(muiX, muiY)))
  {
      muiX = luiX;
      muiY = luiY;
      muiCurrentFrame = (muiCurrentFrame + 1)%4;
  }
  else
  {
    //Draw explosion
    cObject * lcExplosion;
    if (cLevel::Get().IsWall(muiX, muiY))
      lcExplosion = new cExplosion(muiX, muiY);
    else
      lcExplosion = new cExplosion(luiX, luiY);
    cLevel::Get().InsertObject(lcExplosion);  
    cLevel::Get().RemoveObject(macName);
  } 

  
}

//Render the current
void  cBullet::Render()
{
 //Get game graphic properties
  int miTileWidth = cGraphicManager::Get().GetTileWidth();
  int miTileHeight = cGraphicManager::Get().GetTileHeight();
  unsigned int luiXScreenOffset = cGraphicManager::Get().GetXScreenOffset();
  unsigned int luiYScreenOffset = cGraphicManager::Get().GetYScreenOffset();
  unsigned int luiGameHeight = cGraphicManager::Get().GetGameHeight();
 
  if (cLevel::Get().IsWall(muiX, muiY))
  {
    return;
  }

  //Draw the bullet
  cTexture lTexture = cTextureManager::Get().GetTexture("Data/" + msTextureName);
  int tex1=lTexture.GetID();
  glEnable(GL_TEXTURE_2D);
  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  glBindTexture(GL_TEXTURE_2D, tex1);  
  glColor3f(1.0,1.0,1.0);

  float coordx_tile = 0.0f;
  float coordy_tile = 0.0f;

  switch(this->muiCurrentFrame)
  {
  case 0:
    coordx_tile = 0.0f;
    coordy_tile = 0.0f;
    break;
  case 1:
    coordx_tile = 0.5f;
    coordy_tile = 0.0f;
    break;
  case 2:
    coordx_tile = 0.0f;
    coordy_tile = 0.5f;
    break;
  case 3:
    coordx_tile = 0.5f;
    coordy_tile = 0.5f;
    break;
  }
  double cte= 0.375f;

  glBegin(GL_QUADS);    
    glTexCoord2f(coordx_tile, coordy_tile+cte);	
    glVertex2i(muiX*miTileWidth, muiY*miTileHeight);
	  glTexCoord2f(coordx_tile+cte, coordy_tile+cte);	
    glVertex2i((muiX+1)*miTileWidth , muiY*miTileHeight);
	  glTexCoord2f(coordx_tile+cte, coordy_tile);	
    glVertex2i((muiX+1)*miTileWidth, (muiY+1)*miTileHeight);
	  glTexCoord2f(coordx_tile, coordy_tile);	
    glVertex2i(muiX*miTileWidth , (muiY+1)*miTileHeight);
  glEnd();  
}
